/**
 * 获取页面管理数据(扁平化)(以页面router为key)
 * @param {Object} cataData 页面管理数据
 * 返回 { 页面路由: { 页面数据, 页面层级 } }
 * @returns { page10000: { data, hierarchy } }
 */
const getCataDataPageFlat = cataData => {
    const pagesFlat = {}
    const isObject = value => Object.prototype.toString.call(value) === '[object Object]'
    const isArray = value => Object.prototype.toString.call(value) === '[object Array]'

    const forChild = (list, hierarchy) => {
        list.forEach(listItem => {
            if (isObject(listItem) && listItem.hasOwnProperty('router') && listItem.hasOwnProperty('name')) {
                const key = listItem.type === 3 ? listItem.code : listItem.router
                pagesFlat[key] = {
                    data: listItem,
                    hierarchy: [...hierarchy, listItem]
                }
            } else {
                hierarchy.push(listItem)
            }
            Object.values(listItem).forEach(item => {
                if (isArray(item)) forChild(item, hierarchy)
            })
        })
    }

    Object.keys(cataData).forEach(key => {
        if (isArray(cataData[key])) forChild(cataData[key], [])
    })

    return pagesFlat
}

export default getCataDataPageFlat